//
//  VerifyPhoneSceneModel.m
//  BrushFace
//
//  Created by Zzz on 15/12/20.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "VerifyPhoneSceneModel.h"
#import "MeStatus.h"

@implementation VerifyPhoneSceneModel
- (void)sendVerifyPhoneNum:(NSInteger)phoneNum cb:(VerifyPhoneCb)cb
{
//    VerifyPhoneRequest * rq = [VerifyPhoneRequest Request];
//    rq.mobile = phoneNum;
//    rq.PATH = @"/passport/sendcodeforappregister.json";
////    [self sendRequest:rq success:^{
////        cb(VERIFY_PHONE_NUM_SUCCESS,rq.output);
////    } error:^{
////        cb(VERIFY_PHONE_NUM_FAILD,rq.output);
////    }];
    NSDictionary *params = @{@"phone":[NSString stringWithFormat:@"%ld",phoneNum]};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    NSString *requestStr = [NSString stringWithFormat:@"%@/passport/sendcodeforappregister.json",SERVICE_ADRESS];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        if ([[NSString stringWithFormat:@"%@",[json objectForKey:@"rtype"]] integerValue]==0) {
            cb(VERIFY_PHONE_NUM_SUCCESS,json);
        }else{
            cb(VERIFY_PHONE_NUM_FAILD,json);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
    
}

- (void)sendEnsureVerifyPhoneCb:(VerifyPhoneCb)cb{
    VerifyPhoneRequest * rq = [VerifyPhoneRequest Request];
    [self sendRequest:rq success:^{
        NSNumber * rtypeNum = [rq.output objectAtPath:kRecvRType];
        cb(VERIFY_PHONE_NUM_SUCCESS,rtypeNum);
    } error:^{
        NSString * errorMsg = [rq.output objectAtPath:kRecvErrorMsg];;
        cb(VERIFY_PHONE_NUM_FAILD,errorMsg);
    }];
}

- (void)sendCode:(NSString *)code AndPhoneNum:(NSString *)phoneNum ToVerifyPhoneCb:(VerifyPhoneCb)cb{
    NSDictionary *params = @{@"phone":[NSString stringWithFormat:@"%@",phoneNum],@"msgcode":[NSString stringWithFormat:@"%@",code],@"tcode":@"iOS",@"imie":[DeviceInfo fetchIDFA]};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    NSString *requestStr = [NSString stringWithFormat:@"%@/passport/apploginbyphone.json",SERVICE_ADRESS];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        if ([[NSString stringWithFormat:@"%@",[json objectForKey:@"result"]] integerValue]==1) {
             [[MeStatus sharedInstance]saveLoginKey:json[@"loginKey"]];
            [[MeStatus sharedInstance] saveVerifyPhoneKey:@"YES"];
            cb(VERIFY_PHONE_NUM_SUCCESS,json);
           
        }else{
            cb(VERIFY_PHONE_NUM_FAILD,json);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
    
}
@end
